// pages/shopping/shopping.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pricesum:0,
    id:0,
    product:[
      {id:0,name:'华为mate',price:3099,src:'/static/images/temp/cate1.jpg'},
      {id:1,name:'华为mate',price:3099,src:'/static/images/temp/cate2.jpg'},
      {id:2,name:'华为mate',price:3099,src:'/static/images/temp/cate3.jpg'},
      {id:3,name:'华为mate',price:3099,src:'/static/images/temp/cate4.jpg'},
      {id:4,name:'华为mate',price:3099,src:'/static/images/temp/cate5.jpg'},
      {id:5,name:'华为mate',price:3099,src:'/static/images/temp/cate6.jpg'},
      {id:6,name:'华为mate',price:3099,src:'/static/images/temp/cate7.jpg'},
      {id:7,name:'华为mate',price:3099,src:'/static/images/temp/cate8.jpg'},
      {id:8,name:'华为mate',price:3099,src:'/static/images/temp/cate9.jpg'},
      {id:9,name:'华为mate',price:3099,src:'/static/images/temp/cate10.jpg'},
      {id:10,name:'华为mate',price:3099,src:'/static/images/temp/cate11.jpg'},
      {id:11,name:'华为mate',price:3099,src:'/static/images/temp/cate12.jpg'},
    ],
    productlist:[]

  },
  //通过判断改变状态时value值是否为空来来判断是否被选中 
//有值代表选中   为空代表没有选中 
 
    niming:function(e){
      var p = this.data.productlist;
      var id = e.currentTarget.id;
      var pri = 0;
    if (e.detail.value !='') {
      
      this.data.check = 1   //表示选中状态
      for(var i=0;i<p.length;i++){
        if(p[i].id == id){
            pri = this.data.pricesum + p[i].price
        }
      }
    }
    
    else {
      for(var i=0;i<p.length;i++){
        if(p[i].id == id){
            pri = this.data.pricesum - p[i].price 
         
        }
      }
      // this.data.check = 0   //表示未选中
    }
    this.setData({
      pricesum:pri
    })
    },
   
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

    
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var username = wx.getStorageSync('id')

    this.setData({
        id: username
     })
     
    this.setData({
      id:this.data.id.concat(username)
  })
    //  this.productlist.push({product[id]})
    var arr = [];
    console.log(username)

    for(var i=0;i<12;i++){
      if(this.data.product[i].id==username){

        var obj = this.data.product[username];
        arr.push(obj)
      }
    }
    
    this.setData({
      productlist : arr
    })
    console.log(this.data.product)
    console.log(this.data.productlist)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})